package com.fas.core.web.taglib;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;

import com.fas.core.constant.R;
import com.fas.core.util.datadict.DataDic;
import com.fas.core.util.datadict.DataDicKit;
import com.jfinal.plugin.activerecord.Model;

public class SelectTag extends TagSupport {
	private static final long serialVersionUID = 1L;
	private Object list;
	private String id;
	private String name;
	private String value;
	private String emptyOption;
	private String required;
	private String headerKey;
	private String headerValue;
	private String cssClass;
	private String onChange;
	private String listKey;
	private String listValue;

	public List<DataDic> getDataDicList(Long cropid) {
		return DataDicKit.me().getDatadicByGroupTag((String) list, cropid);
	}

	@Override
	public int doStartTag() throws JspException {
		Long corpid =  R.Const.SUPER_CORPID;
		//LoginUser loginUser = (LoginUser) this.pageContext.getSession().getAttribute(Keys.LOGIN_USER);
		//corpid = loginUser.getCorpid();

		JspWriter out = pageContext.getOut();
		StringBuffer selectHtml = new StringBuffer();
		try {
			if (list instanceof String) {
				String definition = (String) list;
				if (StringUtils.isNotBlank(definition) && (definition.startsWith(DataDicKit.me().getDictPrefix()))) {

					DataDic emptyOptionItem = new DataDic(this.getHeaderValue(), this.getHeaderKey());
					List<DataDic> selectItems = new ArrayList<DataDic>();
					String selectText = DataDicKit.me().getDictItemNameByCode(definition, value, corpid);
					if (StringUtils.isBlank(selectText)) {
						selectText = this.getHeaderKey();
						if (!"false".equals(emptyOption)) {
							selectItems.add(emptyOptionItem);
						}
					} else {
						if (!"false".equals(emptyOption)) {
							selectItems.add(emptyOptionItem);
						}
					}
					List<DataDic> translateList = this.getDataDicList(corpid);
					// 如果不允许为空，则默认显示第一个选项
					if ("false".equals(emptyOption) && translateList != null && StringUtils.isBlank(selectText)) {// emptyOption为false,默认选择第一项
						DataDic defaultItem = translateList.get(0);
						if (defaultItem != null) {
							selectText = defaultItem.getName();
							value = defaultItem.getValue();
						}
					}
					selectItems.addAll(translateList);
					selectHtml.append("<select name='").append(name).append("' ");
					// Class设置
					if (StringUtils.isNotBlank(cssClass)) {
						selectHtml.append(" class='").append(cssClass).append("' ");
					}
					// onChange设置
					if (StringUtils.isNotBlank(onChange)) {
						selectHtml.append(" onChange='").append(onChange).append("' ");
					}
					// ID设置
					if (StringUtils.isNotBlank(id)) {
						selectHtml.append(" id='").append(id).append("'>");
					} else {
						selectHtml.append(">");
					}
					for (DataDic item : selectItems) {
						if (item.getName().equals(selectText)) {
							selectHtml.append("<option value='" + item.getValue() + "' selected='selected'>" + item.getName()
									+ "</option>");
						} else {
							selectHtml.append("<option value='" + item.getValue() + "'>" + item.getName() + "</option>");
						}
					}
					selectHtml.append("</select>");
				}
			} else if (list instanceof List) {
				List<?> selectList = (List<?>) list;
				if (selectList != null && selectList.size() > 0) {

					List<DataDic> selectItems = new ArrayList<DataDic>();
					List<DataDic> datas = new ArrayList<DataDic>();
					for (Object selectObj : selectList) {
						Object optionText = "";
						Object optionValue = "";
						if (selectObj instanceof Model) {
							optionText = ((Model<?>) selectObj).get(listKey);
							optionValue = ((Model<?>) selectObj).get(listValue);

						} else {
							optionText = BeanUtils.getProperty(selectObj, listKey);
							optionValue = BeanUtils.getProperty(selectObj, listValue);
						}
						datas.add(new DataDic(String.valueOf(optionValue), String.valueOf(optionText)));
					}

					String selectText = "";
					if (StringUtils.isNotBlank(value)) {
						for (DataDic selectObj : datas) {
							if (selectObj.getValue().equals(value)) {
								selectText = selectObj.getName();
							}
						}
					}

					DataDic emptyOptionItem = new DataDic(this.getHeaderValue(), this.getHeaderKey());
					if (StringUtils.isBlank(selectText)) {
						selectText = this.getHeaderKey();
						if (!"false".equals(emptyOption)) {
							selectItems.add(emptyOptionItem);
						}
					} else {
						if (!"false".equals(emptyOption)) {
							selectItems.add(emptyOptionItem);
						}
					}
					selectItems.addAll(datas);

					// 如果不允许为空，则默认显示第一个选项
					if ("false".equals(emptyOption) && StringUtils.isBlank(selectText)) {// emptyOption为false,默认选择第一项
						DataDic defaultItem = datas.get(0);
						if (defaultItem != null) {
							selectText = defaultItem.getName();
							value = defaultItem.getValue();
						}
					}
					// selectItems.addAll(selectList);
					selectHtml.append("<select name='").append(name).append("' ");

					// Class设置
					if (StringUtils.isNotBlank(cssClass)) {
						selectHtml.append(" class='").append(cssClass).append("' ");
					}

					// onChange设置
					if (StringUtils.isNotBlank(onChange)) {
						selectHtml.append(" onChange='").append(onChange).append("' ");
					}

					// ID设置
					if (StringUtils.isNotBlank(id)) {
						selectHtml.append(" id='").append(id).append("'>");
					} else {
						selectHtml.append(">");
					}

					for (DataDic item : selectItems) {
						if (item.getName().equals(selectText)) {
							selectHtml.append("<option value='" + item.getValue() + "' selected='selected'>" + item.getName()
									+ "</option>");
						} else {
							selectHtml.append("<option value='" + item.getValue() + "'>" + item.getName() + "</option>");
						}
					}
					selectHtml.append("</select>");

				}
			} else {
				selectHtml.append("<select name='").append(name).append("' ");

				// Class设置
				if (StringUtils.isNotBlank(cssClass)) {
					selectHtml.append(" class='").append(cssClass).append("' ");
				}

				// onChange设置
				if (StringUtils.isNotBlank(onChange)) {
					selectHtml.append(" onChange='").append(onChange).append("' ");
				}

				// ID设置
				if (StringUtils.isNotBlank(id)) {
					selectHtml.append(" id='").append(id).append("'>");
				} else {
					selectHtml.append(">");
				}

				selectHtml.append("<option value='" + this.getHeaderValue() + "' selected='selected'>" + this.getHeaderKey() + "</option>");

				selectHtml.append("</select>");
			}

			out.print(selectHtml.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}

		return super.doStartTag();
	}

	public Object getList() {
		return list;
	}

	public void setList(Object list) {
		this.list = list;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getEmptyOption() {
		return emptyOption;
	}

	public void setEmptyOption(String emptyOption) {
		this.emptyOption = emptyOption;
	}

	public String getRequired() {
		return required;
	}

	public void setRequired(String required) {
		this.required = required;
	}

	public String getHeaderKey() {
		return headerKey;
	}

	public void setHeaderKey(String headerKey) {
		this.headerKey = headerKey;
	}

	public String getHeaderValue() {
		return headerValue;
	}

	public void setHeaderValue(String headerValue) {
		this.headerValue = headerValue;
	}

	public String getCssClass() {
		return cssClass;
	}

	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}

	public String getOnChange() {
		return onChange;
	}

	public void setOnChange(String onChange) {
		this.onChange = onChange;
	}

	public String getListKey() {
		return listKey;
	}

	public void setListKey(String listKey) {
		this.listKey = listKey;
	}

	public String getListValue() {
		return listValue;
	}

	public void setListValue(String listValue) {
		this.listValue = listValue;
	}

}
